import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:rc_widget/rc_widget.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';


import 'package:flutter_study/widgets/public/themes/index.dart';

class AppButton extends StatelessWidget {
  const AppButton({
     super.key,
    this.text,
    this.onTap,
    this.margin,
    this.width,
    this.height,
    this.style,
    this.radius,
    this.decoration,
    this.spinKitColor,
    this.spinKitSize,
    this.loading = false,
    this.loadingText,
      });

  final bool loading;
  final String? text;
  final String? loadingText;
  final Color? spinKitColor;
  final double? spinKitSize;
  final void Function()? onTap;
  final BoxDecoration? decoration;
  final EdgeInsetsGeometry? margin;

  final double? width;
  final double? height;
  final double? radius;

  final TextStyle? style;

  double get _radius => radius ?? 8.sp;

  BoxDecoration get _decoration {
    return decoration ?? AppThemes.of().button1;
  }

  factory AppButton.outlined({
    bool loading = false,
    String? text,
    String? loadingText,
    Color? spinKitColor,
    double? spinkitSize,
    void Function()? onTap, 
    BoxDecoration?decoration, 
    EdgeInsetsGeometry? margin, 
    double? width, 
    double? height,
    double? radius,
    TextStyle? style, 
  }){
    return AppButton(
      loading: loading,
      text: text,
      loadingText: loadingText,
      spinKitColor: spinKitColor ?? AppThemes.of().colors.text4,
      spinKitSize: spinkitSize,
      onTap: onTap,
      decoration: decoration ?? AppThemes.of().button2,
      margin: margin,
      width: width,
      height: height,
      radius: radius,
      style: style ?? AppThemes.of().w700Text432,
      );

  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width?? double.infinity,
      height: height ?? 88.h,
      margin:margin,decoration:_decoration,
      child: !loading ? _buildText(context) : _buildLoading(),
    );
  }
  
  Widget _buildText(BuildContext context) {
    return RcInkWell(
      onTap: onTap,
      borderRadius: BorderRadius.circular(_radius),
      child: Container(
        alignment: Alignment.center,
        child: FittedBox(
          child:Text(
            text ?? ' ',
            style:style ?? AppThemes.of().buttonText
          )
        ),
      ),
    );
  }
  
  Widget _buildLoading() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        SpinKitCircle(
          size: spinKitSize ?? 40.sp,
          color: spinKitColor ?? AppThemes.of().colors.buttonSpinkit,
        ),
        SizedBox(width: 8.w),
        Flexible(
          child: FittedBox(
            fit: BoxFit.fitWidth,
            child: Text(
              loadingText ?? '提交中'.tr,
              style: style ?? AppThemes.of().buttonText,
            ),
          ),
        ),
      ],
    );
  }
}

